<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * 
 * User: taj
 * Date: 14-9-29
 * Time: 上午10:29
 * 订单执行商品审核时，传订单物品信息生成出库单
 */
class Controller_Insertdoapi extends Controller
{
    /**
     *通过SKU模糊查询SKU,取前limit条数据
     */
    public function action_insertdo()
    {
        require_once(Kohana::find_file('vendor','phprpc/phprpc_server'));
        $server = new PHPRPC_Server();
        function newerpdeliveryorders($dos)
        {
            $num = count($dos);
            $do_sql ='';
            $user_id = DB::select('id')->from('users')->where('name','=',$dos[0]['user_name'])->execute()->get('id',1);

            if($num<2){
                return false;
            }else if(empty($dos[0]['order_id'])){
               return false;
           }else{

               for($i=1;$i<$num;$i++){
                   if($dos[$i]['sku']=='' || empty($dos[$i]['item_count'])){
                       //return false;
                       continue;
                   }

                   $saleAccount = isset($dos[$i]['sales_account']) ? $dos[$i]['sales_account'] : 0;

                   $do_sql .= "('".date('Y-m-d H:i:s')."','".date('Y-m-d H:i:s')."','".$dos[0]['order_id']."','".
                       $dos[$i]['ordersproduct_id']."','".$saleAccount."','".intval($user_id)."','".mysql_escape_string(strtoupper(trim($dos[$i]['sku']))).
                       "','".mysql_escape_string(trim($dos[$i]['sku_name']))."','','','".
                       intval($dos[$i]['item_count'])."',0,0,0,'订单正常销售出库',10),";
               }
                $sql_update="INSERT INTO `deliveryorders`(`delivery_date`, `op_time`, `order_id`, `orders_products_id`, `delivery_department`,
                        `delivery_man`,`sku`, `sku_name`, `sku_standard`, `storage_position`, `quantity`, `single_price`,
                        `money`, `cost`,  `remark`, `type`) VALUES ";
                if($do_sql!=''){
                    $sql_update .= rtrim($do_sql,",");
                    list($insert_id, $total_rows) = DB::query(Database::INSERT,$sql_update)->execute('newerp');
                }
                if($total_rows>0)return true;
                else return false;
           }
        }

        $server->add('newerpdeliveryorders');
        $server->start();
    }
}
